﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;


namespace iEN.PlatformSet
{
    public partial class SetUser : System.Web.UI.Page
    {
        static DataTable gvBackup = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["SysAdmin"] != null)
                {
                    initUserList();
                }
                else
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }
            }


        }

        private void initUserList()
        {
            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                lblMsg.Visible = true;
            }
            
            DataTable gvResult = new DataTable();

            DataColumn column;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Account";
            column.ReadOnly = true;
            gvResult.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Username";
            column.ReadOnly = true;
            gvResult.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Mobilephone";
            column.ReadOnly = true;
            gvResult.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Type";
            column.ReadOnly = true;
            gvResult.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "Email";
            column.ReadOnly = true;
            gvResult.Columns.Add(column);

            // Fill up gvResult
            DataTable dt = new DBAccess().getUserData("1");

            if (dt != null)
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string typeName = "";

                    switch (int.Parse(dt.Rows[i]["_type"].ToString()))
                    {
                        case 0:
                            typeName = "Guest";
                            break;
                        case 1:
                            typeName = "Operator";
                            break;
                        case 2:
                            typeName = "Director";
                            break;
                        default:
                            break;
                    }

                    gvResult.Rows.Add(dt.Rows[i]["_account"].ToString(),
                                        dt.Rows[i]["_name"].ToString(),
                                        dt.Rows[i]["_phone_mobile"].ToString(),
                                        typeName,
                                        dt.Rows[i]["_mail"].ToString());
                }

            gvUser.DataSource = gvResult;
            gvUser.DataBind();

            gvBackup = gvResult;

            Session["UserDetailData"] = dt;
        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCommand")
            {
                int index = Convert.ToInt32(e.CommandArgument);

                GridViewRow row = gvUser.Rows[index];

                bool result = new DBAccess().deleteUserData(gvBackup.Rows[row.RowIndex]["Account"].ToString());

                this.Page.Response.Redirect("SetUser.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.Page.Response.Redirect("CreateUser.aspx");
        }

    }
}